package com.example.base.utils;

import android.annotation.SuppressLint;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;

import com.example.base.R;
import com.example.base.widget.JudgeNestedScrollView;
import com.google.android.material.appbar.AppBarLayout;


/**
 * Author by zhengbenying
 * Email zhengbenying@hikvison.com
 * Company 杭州海康威视数字技术股份有限公司
 * Date on 2018/9/6
 */
public class BaseFindView {
    private View view;
    private TextView title;
    private ImageView back;
    private ImageView headIv;
    private RelativeLayout rlTitle;
    private TextView mTvToolbarTitle;
    private Toolbar mToolBar;
    private AppCompatActivity context;
    private AppBarLayout barLayout;
    public JudgeNestedScrollView mNestedScrollView;
    private float mScaleX;
    private float mScaleY;
    /***
     * 状态栏高度，如果没沉浸状态下则为0
     */
    private int statusBarHeight;

    public BaseFindView(View view, final AppCompatActivity context) {
        this.view = view;
        this.context = context;
        title = view.findViewById(R.id.base_head_title_tv);
        back = view.findViewById(R.id.back_iv);
        headIv = view.findViewById(R.id.ny_safeproduction_common_head_iv);
        mToolBar = view.findViewById(R.id.ny_safeproduction_common_toolbar);
        rlTitle = view.findViewById(R.id.base_head_title_rl);
        mTvToolbarTitle = view.findViewById(R.id.ny_safeproduction_common_toolbar_title_tv);
        barLayout = view.findViewById(R.id.ny_safrproduction_common_app_bar);
        mNestedScrollView = view.findViewById(R.id.ny_safeproduction_inspection_base_body);
        view.findViewById(R.id.back_ib).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.finish();
            }
        });
    }

    /**
     * 设置标题
     *
     * @param titleStr
     */
    public void setTitle(String titleStr) {
        title.setText(titleStr);
        mTvToolbarTitle.setText(titleStr);
    }

    /**
     * 设置标题颜色
     */
    public void setWhiteColor(boolean isWhite) {
        if (isWhite) {
            title.setTextColor(context.getResources().getColor(R.color.ny_safeproduction_base_white));
            mTvToolbarTitle.setTextColor(context.getResources().getColor(R.color.ny_safeproduction_base_white));
            back.setImageResource(R.mipmap.ny_safeproduction_common_back_white);
        } else {
            title.setTextColor(context.getResources().getColor(R.color.ny_safeproduction_base_black));
            mTvToolbarTitle.setTextColor(context.getResources().getColor(R.color.ny_safeproduction_base_black));
            back.setImageResource(R.mipmap.ny_safeproduction_common_back_black);
        }
    }

    /**
     * 设置头部图片背景
     *
     * @param resourceid
     */
    public void setHeadImageResource(int resourceid) {
        setStatusImage();
        headIv.setImageResource(resourceid);
    }

    /**
     * 取消头部
     */
    public void setTitleView(View headView) {
//        rlTitle.setVisibility(View.GONE);
//        barLayout.setVisibility(View.GONE);
        mToolBar.removeAllViews();
        mToolBar.addView(headView);
    }

    /**
     * 设置沉浸栏需要动态增加appbarlayout高度
     */
    private void setStatusImage() {
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
            float appBarHeight = context.getResources().getDimension(R.dimen.dp_100);
            appBarHeight = appBarHeight + statusBarHeight;
            barLayout.setLayoutParams(new CoordinatorLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) appBarHeight));
            rlTitle.setLayoutParams(new CoordinatorLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) appBarHeight));
        }
    }

    /**
     * 将appbar置顶
     */
    public void setTitleTop() {
        barLayout.setExpanded(false);
        rlTitle.setVisibility(View.GONE);
        mTvToolbarTitle.setVisibility(View.VISIBLE);
    }

    @SuppressLint("NewApi")
    public void initHead() {
        title.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                title.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                scale();
            }
        });
    }

    @SuppressLint("NewApi")
    private void scale() {
        final float screenW = context.getResources().getDisplayMetrics().widthPixels;
        final float toolbarHeight = context.getResources().getDimension(R.dimen.dp_44);
        final float appBarHeight = context.getResources().getDimension(R.dimen.dp_44);
        //得到高度差
        final float distanceY = title.getY() - (toolbarHeight - title.getHeight()) / 2.0f - statusBarHeight;
        barLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                //得到水平差值  屏幕宽度一半 - 按钮宽度一半
                float distanceX = screenW / 2.0f - (title.getWidth() / 2.0f) - context.getResources().getDimension(R.dimen.dp_17);
                mScaleX = distanceX / (appBarHeight - toolbarHeight);
                mScaleY = distanceY / (appBarHeight - toolbarHeight);
                //得到缩放
                float scale = 1.0f - (-verticalOffset) / (appBarHeight - toolbarHeight);
                title.setScaleX((float) (0.7 + 0.3 * scale));
                title.setScaleY((float) (0.7 + 0.3 * scale));
                title.setTranslationX(-mScaleX * verticalOffset);
                title.setTranslationY((mScaleY * verticalOffset));

                boolean isFold = isFold(verticalOffset, (int) (appBarHeight - toolbarHeight));

                if (isFold) {
                    hideAppBool();
                }
            }
        });
    }

    /**
     * 隐藏appbar
     */
    public void hideAppBool() {
        View toolbar = barLayout.findViewById(R.id.ny_safeproduction_common_collapsingToolbarLayout);
        AppBarLayout.LayoutParams params =
                (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
        params.setScrollFlags(0);
        toolbar.setLayoutParams(params);

        CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) barLayout.getLayoutParams();
        layoutParams.height = (int) context.getResources().getDimension(R.dimen.dp_44) + statusBarHeight;
        barLayout.setLayoutParams(layoutParams);

        rlTitle.setVisibility(View.GONE);
        mTvToolbarTitle.setVisibility(View.VISIBLE);
    }


    private boolean isFold(int verticalOffset, int toolbarHeight) {
        boolean isFold = false;
        if (toolbarHeight == Math.abs(verticalOffset)) {
            isFold = true;
        }
        return isFold;
    }
}
